package cn.iocoder.yudao.module.agri.controller.admin.farmmodelstagemission;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.agri.controller.admin.farmmodelstagemission.vo.*;
import cn.iocoder.yudao.module.agri.dal.dataobject.farmmodelstagemission.FarmModelStageMissionDO;
import cn.iocoder.yudao.module.agri.convert.farmmodelstagemission.FarmModelStageMissionConvert;
import cn.iocoder.yudao.module.agri.service.farmmodelstagemission.FarmModelStageMissionService;

@Tag(name = "管理后台 - 种养模型-阶段任务")
@RestController
@RequestMapping("/agri/farm-model-stage-mission")
@Validated
public class FarmModelStageMissionController {

    @Resource
    private FarmModelStageMissionService farmModelStageMissionService;

    @PostMapping("/create")
    @Operation(summary = "创建种养模型-阶段任务")
    @PreAuthorize("@ss.hasPermission('agri:farm-model-stage-mission:create')")
    public CommonResult<Long> createFarmModelStageMission(@Valid @RequestBody FarmModelStageMissionCreateReqVO createReqVO) {
        return success(farmModelStageMissionService.createFarmModelStageMission(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新种养模型-阶段任务")
    @PreAuthorize("@ss.hasPermission('agri:farm-model-stage-mission:update')")
    public CommonResult<Boolean> updateFarmModelStageMission(@Valid @RequestBody FarmModelStageMissionUpdateReqVO updateReqVO) {
        farmModelStageMissionService.updateFarmModelStageMission(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除种养模型-阶段任务")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('agri:farm-model-stage-mission:delete')")
    public CommonResult<Boolean> deleteFarmModelStageMission(@RequestParam("id") Long id) {
        farmModelStageMissionService.deleteFarmModelStageMission(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得种养模型-阶段任务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('agri:farm-model-stage-mission:query')")
    public CommonResult<FarmModelStageMissionRespVO> getFarmModelStageMission(@RequestParam("id") Long id) {
        FarmModelStageMissionDO farmModelStageMission = farmModelStageMissionService.getFarmModelStageMission(id);
        return success(FarmModelStageMissionConvert.INSTANCE.convert(farmModelStageMission));
    }

    @GetMapping("/list")
    @Operation(summary = "获得种养模型-阶段任务列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('agri:farm-model-stage-mission:query')")
    public CommonResult<List<FarmModelStageMissionRespVO>> getFarmModelStageMissionList(@RequestParam("ids") Collection<Long> ids) {
        List<FarmModelStageMissionDO> list = farmModelStageMissionService.getFarmModelStageMissionList(ids);
        return success(FarmModelStageMissionConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得种养模型-阶段任务分页")
    @PreAuthorize("@ss.hasPermission('agri:farm-model-stage-mission:query')")
    public CommonResult<PageResult<FarmModelStageMissionRespVO>> getFarmModelStageMissionPage(@Valid FarmModelStageMissionPageReqVO pageVO) {
        PageResult<FarmModelStageMissionDO> pageResult = farmModelStageMissionService.getFarmModelStageMissionPage(pageVO);
        return success(FarmModelStageMissionConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出种养模型-阶段任务 Excel")
    @PreAuthorize("@ss.hasPermission('agri:farm-model-stage-mission:export')")
    @OperateLog(type = EXPORT)
    public void exportFarmModelStageMissionExcel(@Valid FarmModelStageMissionExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<FarmModelStageMissionDO> list = farmModelStageMissionService.getFarmModelStageMissionList(exportReqVO);
        // 导出 Excel
        List<FarmModelStageMissionExcelVO> datas = FarmModelStageMissionConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "种养模型-阶段任务.xls", "数据", FarmModelStageMissionExcelVO.class, datas);
    }

}
